package hust.edu.Controller;

import hust.edu.Model.User;
import hust.edu.Service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/user")
public class UserController {
	@Autowired
	private UserService userService;
	
	
	@RequestMapping("/login")
	public String login(User user){
		Subject sbj = SecurityUtils.getSubject();
		if(sbj.isAuthenticated()){
			return "success";
		}else{
			UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword(), true);
			//shiro 登录验证
		    try {  
		        SecurityUtils.getSubject().login(token);  
		        return "success";
		    } catch (Exception ex) {  
		        ex.printStackTrace();  
		        return "fail";  
		    }  
		}
	}
	
	
	@RequestMapping("/logout")
	public String logout(){
		Subject sbj = SecurityUtils.getSubject();
		sbj.logout();
		return "redirect:/login.jsp";
	}
	
	@RequestMapping("/setLocale")
	public String setLocale(@RequestParam("locale")String locale){
//		Locale _locale;
//		if(locale.equals("zh")){
//			_locale = new Locale("zh", "CN");
//		}else if(locale.equals("en")){
//			_locale = new Locale("en", "US");
//		}else{
//			_locale = LocaleContextHolder.getLocale();
//		}
//		req.getSession().setAttribute(SessionLocaleResolver.LOCALE_SESSION_ATTRIBUTE_NAME, _locale);	
		return "login";
	}


}
